<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include  file="../jsp/userControl.jsp"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	String courseId = request.getParameter("courseId");
	request.setAttribute("courseId", courseId);
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 5.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">


<link rel="stylesheet" type="text/css" href="css/resourceUpload.css" />
<style type="text/css">
.file {
	filter: alpha(opacity :     0);
	opacity: 0;
}
</style>
<script>
//上传文件
function openFile(){
	 document.getElementById("fileField").click();
}

//显示选择文件名
function showFileName(fileField){
	document.getElementById('textfield').value=fileField.value;
	file_upl.select();    
	alert(document.selection.createRange().text);
}

//数据检验：文件上传信息
function form_submit(form) {
	recoverFlag();
	if (checkResourceName(form) & checkResourceDescribe(form) & checkFextfield(form)) {
		return true;
	} else {
		return false;
	}
}

//重置按钮
function recoverFlag() {
	var span1 = document.getElementById("resourceNameSpanId");
	var span2 = document.getElementById("resourceDescribeSpanId");
	var span3 = document.getElementById("textfieldSpanId");
	span1.innerHTML = "*";
	span2.innerHTML = "*";
	span3.innerHTML = "";
}

//数据校验：校验作业名
function checkResourceName(form){//必填，1-30个字母数字汉字或下划线
	var resourceName = form.resourceName;
	var resourceName_value = form.resourceName.value;
	var span = document.getElementById("resourceNameSpanId");
	var regex = /^[a-zA-Z0-9_\u4e00-\u9fa5]{1,30}$/;
	if (regex.test(resourceName_value)) {
		span.innerHTML="<img src='images/ok.gif'/>";
		return true;
	} else {
		span.innerHTML = "<img src='images/no.gif'/><font color='red'>必填,且由1-30个汉字、字母、数字、下划线组成</font>";
		resourceName.focus();
		return false;
	}
}

//数据校验：校验描述
function checkResourceDescribe(form){//必填，1-60个字母数字汉字或下划线
	var resourceDescribe = form.resourceDescribe;
	var resourceDescribee_value = form.resourceDescribe.value;
	var span = document.getElementById("resourceDescribeSpanId");
	var regex = /^[a-zA-Z0-9_\u4e00-\u9fa5]{1,30}$/;
	if (regex.test(resourceDescribee_value)) {
		span.innerHTML="<img src='images/ok.gif'/>";
		return true;
	} else {
		span.innerHTML = "<img src='images/no.gif'/><font color='red'>必填,且由1-60个汉字、字母、数字、下划线组成</font>";
		resourceDescribe.focus();
		return false;
	}
}

//数据校验：校验路径
function checkFextfield(form){//必填，1-60个字母数字汉字或下划线
	var textfield = form.textfield;
	var textfield_value = form.textfield.value;
	var span = document.getElementById("textfieldSpanId");
	var regex = /^[a-zA-Z]{1,2}:[a-zA-Z_0-9\u4e00-\u9fa5\\.]*$/;
	if (regex.test(textfield_value)) {
		span.innerHTML="<img src='images/ok.gif'/>";
		return true;
	} else {
		span.innerHTML = "<img src='images/no.gif'/><font color='red'>必填,文件路径错误</font>";
		textfield.focus();
		return false;
	}
}
</script>

</head>

<body>
	<form class="contact_form"
		action="resourceUpload?courseId=${courseId}"
		method="post" name="contact_form" enctype="multipart/form-data"
		onsubmit="return form_submit(contact_form);">
		<ul>
			<li>
				<h2>上传资源</h2>
			</li>
			<li>
				<label for="name">资源名:</label> 
				<input type="text" name="resourceName" maxlength="30" /><span id="resourceNameSpanId"> *</span>
			</li>
			<li>
				<label for="message">描述:</label> 
				<textarea name="resourceDescribe" rows="10" cols="20"></textarea><span id="resourceDescribeSpanId"> *</span>
			</li>
			<li>
				<label for="message">文件:</label> 
				<input type="text" 	name="textfield" id="textfield" class="txt" maxlength="40" />
				<input type="button" name="btn" class="btn" value="浏览..."
				style="width:55px;height:33px" onclick="openFile()" /> 
				<span id="textfieldSpanId"></span>
				<input type="file" name="fileField" class="file" id="fileField" size="28"
				onchange="showFileName(this)" /></li>
			<li>
				<button class="submit" type="submit">上传</button>
				<button class="submit" type="reset" onclick="recoverFlag()">重置</button>	
			</li>
			<li><div style="color:red">${request.message }</div></li>
		</ul>
	</form>
</body>
</html>
